<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/css" href="chapter.css"?><chapter>
  <title>Chapter 11:	Namespaces</title>
  <simplesect>
    <para>At this point, you may be wondering why we need a chapter about namespaces; after all since the very first example our schemas include an attribute from the xml:lang namespace and that doesn't seem like a big deal.</para>
    <para>If you think about it more carefully, you'll see that namespaces are presenting two different challenges to schema languages. The first is syntactical: schema languages need to provide a way to define to which namespaces belong the elements and attributes which are described; the second is how schema languages can cope with the extensibility which is one of the objectives of XML namespaces.</para>
    <para>In this chapter, we'll have a closer look at these two challenges before seeing how Relax NG addresses them.</para>
  </simplesect>
  <sect1>
    <title>A ten minutes guide to XML namespaces</title>
    <para>Let's go back to the motivations beyond XML Namespaces. The first and basic one is to be a kind of replacement for the XML (SGML inherited) doctype public identifier and provide an way to identify which vocabulary, i.e. which set of names is being used in a document. The XML/SGML way of identifying the vocabulary used in our library would be to add a public identifier in the document type declaration such as:</para>
    <programlisting>
<![CDATA[ <?xml version="1.0"?>
 <!DOCTYPE library PUBLIC "-//ERICVANDERVLIST//DTD for library//EN" "library.dtd"/>
 <library>
 .../...
 </library>]]>
    </programlisting>
    <para>One of the downsides of this method is that it doesn't totally decouple the identification of the vocabulary (&quot;-//ERICVANDERVLIST//DTD for library//EN&quot;) from the location of the DTD describing the vocabulary (&quot;library.dtd&quot;): I can't identify without giving the location! This is normal since in fact, the identification is the identification of the DTD rather than the identification of the abstract set of names.</para>
    <para>The first goal of XML namespaces is to provide identifiers for this abstract notion of &quot;vocabulary&quot;, &quot;set of names&quot; or simply &quot;namespaces&quot; without linking these identifiers to any technical implementation (DTD, schemas or whatever) defining or enforcing what they are. These identifiers are no longer public identifiers like those used in doctype declarations but URIs (or rather to be picky &quot;URI references&quot;) and to assign a namespace to all the elements from our example, we could write:</para>
    <programlisting>
<![CDATA[ <?xml version="1.0"?>
 <library xmlns="http://eric.van-der-vlist.com/ns/library">
 .../...
 </library>]]>
    </programlisting>
    <para>The identifier for my namespace is the string &quot;http://eric.van-der-vlist.com/ns/library&quot; and this address doesn't have to host any document. At the time I am writing these lines, this address doesn't lead to any web page at all and if you copy it into your favorite web browser, you'd just get a &quot;404 Not Found&quot; error. The assumption is just that I use it only if I own the domain and that I won't use it for several different namespaces. Later on, I could publish something at that location, either a documentation (formal or not) or any kind of schema; that's not forbidden by the namespaces recommendation, this can be useful but the whole subject is highly controversial. Also note that XML namespaces per see do not define any way to associate resources such as schemas or documentations with a namespace URI.</para>
    <para>The namespace declaration (xmlns=&quot;http://eric.van-der-vlist.com/ns/library&quot;) has been done for the document element (&quot;library&quot;) and is inherited by all its children elements.</para>
    <para>The second goal of XML namespaces is to provide a way to mix elements and attributes from different namespaces in a single document. In our library for instance, the library and book elements use a vocabulary specific to libraries while the author element could use a vocabulary for human resources and the character element be a mixed of both: the character element itself and the qualification element would be from the library namespace while the <literal>name</literal> and <literal>born</literal> elements would be from the HR vocabulary.</para>
    <para>Leveraging on declaration inheritance, this could be achieved using the <literal>xmlns</literal> declaration has we have already seen:</para>
    <programlisting>
<![CDATA[ <?xml version="1.0"?>
 <library xmlns="http://eric.van-der-vlist.com/ns/library">
  <book id="b0836217462" available="true">
   <isbn>0836217462</isbn>
   <title xml:lang="en">Being a Dog Is a Full-Time Job</title>
   <author id="CMS" xmlns="http://eric.van-der-vlist.com/ns/person">
    <name>Charles M Schulz</name>
    <born>1922-11-26</born>
    <died>2000-02-12</died>
   </author>
   <character id="PP">
    <name xmlns="http://eric.van-der-vlist.com/ns/person">Peppermint Patty</name>
    <born xmlns="http://eric.van-der-vlist.com/ns/person">1966-08-22</born>
    <qualification>bold, brash and tomboyish</qualification>
    </character>
   <character id="Snoopy">
    <name xmlns="http://eric.van-der-vlist.com/ns/person">Snoopy</name>
    <born xmlns="http://eric.van-der-vlist.com/ns/person">1950-10-04</born>
    <qualification>extroverted beagle</qualification>
   </character>
   <character id="Schroeder">
    <name xmlns="http://eric.van-der-vlist.com/ns/person">Schroeder</name>
    <born xmlns="http://eric.van-der-vlist.com/ns/person">1951-05-30</born>
    <qualification>brought classical music to the Peanuts strip</qualification>
   </character>
   <character id="Lucy">
    <name xmlns="http://eric.van-der-vlist.com/ns/person">Lucy</name>
    <born xmlns="http://eric.van-der-vlist.com/ns/person">1952-03-03</born>
    <qualification>bossy, crabby and selfish</qualification>
   </character>
  </book>
 </library>]]>
    </programlisting>
    <para>But we see that it would rapidly become very verbose and XML namespaces provide a way to assign prefixes to namespaces which can then be used to prefix the names of the elements (and attributes) to declare their namespaces. The namespace declared using the <literal>xmlns</literal> attribute is called the &quot;default namespace&quot; since it is assigned to elements which have no prefix. The document above could be rewritten using the default namespace for the library and assigning a prefix to the other namespace:</para>
    <programlisting>
<![CDATA[ <?xml version="1.0"?>
 <library
   xmlns="http://eric.van-der-vlist.com/ns/library"
   xmlns:hr="http://eric.van-der-vlist.com/ns/person">
  <book id="b0836217462" available="true">
   <isbn>0836217462</isbn>
   <title xml:lang="en">Being a Dog Is a Full-Time Job</title>
   <hr:author id="CMS">
    <hr:name>Charles M Schulz</hr:name>
    <hr:born>1922-11-26</hr:born>
    <hr:died>2000-02-12</hr:died>
   </hr:author>
   <character id="PP">
    <hr:name>Peppermint Patty</hr:name>
    <hr:born>1966-08-22</hr:born>
    <qualification>bold, brash and tomboyish</qualification>
    </character>
   <character id="Snoopy">
    <hr:name>Snoopy</hr:name>
    <hr:born>1950-10-04</hr:born>
    <qualification>extroverted beagle</qualification>
   </character>
   <character id="Schroeder">
    <hr:name>Schroeder</hr:name>
    <hr:born>1951-05-30</hr:born>
    <qualification>brought classical music to the Peanuts strip</qualification>
   </character>
   <character id="Lucy">
    <hr:name>Lucy</hr:name>
    <hr:born>1952-03-03</hr:born>
    <qualification>bossy, crabby and selfish</qualification>
   </character>
  </book>
 </library>]]>
    </programlisting>
    <para>The value of the prefix (<literal>hr</literal> in this example) is chosen arbitrarily by the author of the XML document and should be considered as not significant by applications conform the the namespaces specification. Here, I have chosen <literal>hr</literal> standing for &quot;Human Resources&quot; to make it easier to remember for human readers, but I could have chosen any other value (such as <literal>foo</literal>) for this prefix as long as, of course, the prefix matches its definition.</para>
    <para>We could also prefer, for symmetry, use a prefix for both namespaces:</para>
    <programlisting>
<![CDATA[ <?xml version="1.0"?>
 <lib:library
   xmlns:lib="http://eric.van-der-vlist.com/ns/library"
   xmlns:hr="http://eric.van-der-vlist.com/ns/person">
  <lib:book id="b0836217462" available="true">
   <lib:isbn>0836217462</lib:isbn>
   <lib:title xml:lang="en">Being a Dog Is a Full-Time Job</lib:title>
   <hr:author id="CMS">
    <hr:name>Charles M Schulz</hr:name>
    <hr:born>1922-11-26</hr:born>
    <hr:died>2000-02-12</hr:died>
   </hr:author>
   <lib:character id="PP">
    <hr:name>Peppermint Patty</hr:name>
    <hr:born>1966-08-22</hr:born>
    <lib:qualification>bold, brash and tomboyish</lib:qualification>
    </lib:character>
   <lib:character id="Snoopy">
    <hr:name>Snoopy</hr:name>
    <hr:born>1950-10-04</hr:born>
    <lib:qualification>extroverted beagle</lib:qualification>
   </lib:character>
   <lib:character id="Schroeder">
    <hr:name>Schroeder</hr:name>
    <hr:born>1951-05-30</hr:born>
    <lib:qualification>brought classical music to the Peanuts strip</lib:qualification>
   </lib:character>
   <lib:character id="Lucy">
    <hr:name>Lucy</hr:name>
    <hr:born>1952-03-03</hr:born>
    <lib:qualification>bossy, crabby and selfish</lib:qualification>
   </lib:character>
  </lib:book>
 </lib:library>]]>
    </programlisting>
    <para>Note that for a namespace aware application, these three documents are considered equivalent:  the prefixes are only shortcuts to associate a namespace URI and a &quot;local name&quot; (the part of the name which is after the colon) to disambiguate this name from eventual synonyms defined in other namespaces.</para>
    <para>Up to now, we've spoken of elements and attributes are given a similar yet special treatment. Similar in that attribute names can be prefixed to show that they belong to a namespace but special since the default namespace doesn't apply to them and that the attributes which have no prefix are considered to have no namespace URI but still &quot;belong&quot; to the namespace of their parent element. The reason for this is that attributes are supposed to be used to provide meta-data qualifying their parent element rather than to contain actual information and being qualifiers, it has been considered that by default they &quot;belong&quot; to the same vocabulary than their parent elements. This is the reason why I have kept the <literal>id</literal> and <literal>available</literal> attributes without prefix in my there examples.</para>
    <para>The last goal of XML namespaces (and the motivation for taking that much pain to allow several namespaces in a single document) is to facilitate the development of independent (or semi-independent) vocabularies which can be used as building blocks. One of the ideas is that if applications are cleanly developed and just drop elements and attributes which they don't understand documents can be extended without breaking existing applications.</para>
    <para>For instance, in our library we've not defined the publisher of the book. We can add a <literal>publisher</literal> element in our namespace, but instead we might want to use the definition given by the Dublin Core Metadata Initiative (DCMI) and use their namespaces to write:</para>
    <programlisting>
<![CDATA[ <?xml version="1.0"?>
 <library
   xmlns="http://eric.van-der-vlist.com/ns/library"
   xmlns:hr="http://eric.van-der-vlist.com/ns/person"
   xmlns:dc="http://purl.org/dc/elements/1.1/">
  <book id="b0836217462" available="true">
   <isbn>0836217462</isbn>
   <title xml:lang="en">Being a Dog Is a Full-Time Job</title>
   <dc:publisher>Andrews Mc Meel Publishing</dc:publisher>
   .../...
  </book>
 </library>]]>
    </programlisting>
    <para>A double benefit is expected from doing so. Everyone understands that the <literal>publisher</literal> element is corresponding to the definition given by the DCMI:</para>
    <programlisting>
<![CDATA[    URI:            http://purl.org/dc/elements/1.1/publisher
    Namespace:      http://purl.org/dc/elements/1.1/
    Name:           publisher
    Label:          Publisher
    Definition:     An entity responsible for making the resource available
    Comment:        Examples of a Publisher include a person, an organization,
                    or a service.
                    Typically, the name of a Publisher should be used to
                    indicate the entity.
    Type of term:   http://dublincore.org/usage/documents/principles/#element
    Status:         http://dublincore.org/usage/documents/process/#recommended
    Date issued:    1998-08-06
    Date modified:  2002-10-04
    Decision:       http://dublincore.org/usage/decisions/#Decision-2002-03
    This version:   http://dublincore.org/usage/terms/dc/#publisher-004
    Replaces:       http://dublincore.org/usage/terms/dc/#publisher-003]]>
    </programlisting>
    <para>Again, note that the mechanism to retrieve this definition is not specified by the XML namespace recommendation. The second benefit is that if my application has been implemented to skip elements and attributes from unsupported namespaces, the addition of this <literal>dc:publisher</literal> element won't break anything.</para>
  </sect1>
  <sect1>
    <title>The two challenges of namespaces</title>
    <para>The progression followed in our &quot;10 minutes guide to namespaces&quot; has carefully be designed to let you guess what these two challenges are and you'll probably have already seen where I wanted to go!</para>
    <para>The first issue is to provide the ad-hoc mechanisms to associate namespace URIs to patterns describing elements and attributes and this will be described in the next section.</para>
    <para>The second issue is to provide mechanisms to write extensible schemas for those applications skipping unknown namespaces. Of course, writing extensible schemas is not limited to multi namespaces documents and we will see more of it in our next chapter, but we will start introducing the mechanism called &quot;name classes&quot; which is the key to extensibility with Relax NG in the last section of this chapter to fully cover the case of namespaces.</para>
  </sect1>
  <sect1>
    <title>Namespace declarations</title>
    <para>Namespace declarations in a Relax NG schema are following the same principles as namespace declarations in an instance document with some differences in the syntax and we will also find both the possibility to use default namespaces and the possibility to use prefixes.</para>
    <sect2>
      <title>Using default namespaces</title>
      <para>The namespace expected in the instance document can be defined through the <literal>ns</literal> attribute which is an &quot;inherited&quot; attribute like the <literal>datatypeLibrary</literal> attribute seen before. Being inherited means that we can define it in the document element of the schema if it remains the same all over the schema. For instance, to write a schema for the example where all the library is using the same namespace, we could write:</para>
      <programlisting>
<![CDATA[ <?xml version="1.0" encoding="utf-8"?>
 <element xmlns="http://relaxng.org/ns/structure/1.0" name="library"
          ns="http://eric.van-der-vlist.com/ns/library">
  <oneOrMore>
   <element name="book">
    <attribute name="id"/>
    .../...
   </element>
  </oneOrMore>
 </element>]]>
      </programlisting>
      <para>Or, using the compact syntax:</para>
      <programlisting>
<![CDATA[ default namespace = "http://eric.van-der-vlist.com/ns/library"]]>
      </programlisting>
      <programlisting>
<![CDATA[
 element library
 {
    element book
    {
       attribute id { text },
       .../...
       }*
    }+
 }]]>
      </programlisting>
      <para>Note that the definition of the default namespace in a Relax NG schema do not apply to attributes (exactly as the default namespace doesn't apply to attributes in instance documents).</para>
      <para>Exactly as default namespaces could be used and changed all over a multi namespace document, when we are using the XML syntax the <literal>ns</literal> attribute can be changed in a schema and to validate the documents with two namespaces show in our &quot;10 minutes guide to namespaces&quot;, we could write:</para>
      <programlisting>
<![CDATA[ <?xml version="1.0" encoding="utf-8"?>
 <element xmlns="http://relaxng.org/ns/structure/1.0"
  name="library"
  ns="http://eric.van-der-vlist.com/ns/library">
  <oneOrMore>
   <element name="book">
    <attribute name="id"/>
    <attribute name="available"/>
    <element name="isbn">
     <text/>
    </element>
    <element name="title">
     <attribute name="xml:lang"/>
     <text/>
    </element>
    <zeroOrMore>
     <element name="author" ns="http://eric.van-der-vlist.com/ns/person">
      <attribute name="id"/>
      <element name="name">
       <text/>
      </element>
      <element name="born">
       <text/>
      </element>
      <optional>
       <element name="died">
        <text/>
       </element>
      </optional>
     </element>
    </zeroOrMore>
    <zeroOrMore>
     <element name="character">
      <attribute name="id"/>
      <element name="name" ns="http://eric.van-der-vlist.com/ns/person">
       <text/>
      </element>
      <element name="born" ns="http://eric.van-der-vlist.com/ns/person">
       <text/>
      </element>
      <element name="qualification">
       <text/>
      </element>
     </element>
    </zeroOrMore>
   </element>
  </oneOrMore>
 </element>]]>
      </programlisting>
      <para>The compact syntax does not provide such a way to redefine the default namespace and defining prefixes will be the preferred way to define schemas with multiple namespaces when using the compact syntax.</para>
      <para>Since the three variations used to write the document with the two namespaces in our &quot;10 minutes guide&quot; are considered equivalent for namespaces aware applications, the schema which we've just written will validate them indifferently. There is thus a complete independence between the prefixes and default namespaces used to write the instance document and those used in the schema and the match between namespaces is only done through matching the namespace URIs of each element and attribute.</para>
    </sect2>
    <sect2>
      <title>Using prefixes</title>
      <para>Although the definition of the default target namespace in a Relax NG is done through a <literal>ns</literal> attribute and thus do not rely on the declaration of the default namespace of the Relax NG document itself (in our examples, the default namespace of the Relax NG document is the Relax NG namespace), the declaration of the prefixes used as shortcut to the non default target namespaces is done through namespaces declarations. In other words, to define a <literal>hr</literal> prefix which will be used as a prefix for the namespaces in names or attributes of the instance, I declare this prefix through a <literal>xmlns:hr</literal> declaration as if I wanted to use it as a prefix for an element or attribute of the Relax NG document.</para>
      <para>We can mix both default and non default namespaces in our schemas and write:</para>
      <programlisting>
<![CDATA[ <?xml version="1.0" encoding="utf-8"?>
 <element xmlns="http://relaxng.org/ns/structure/1.0"
  name="library"
  ns="http://eric.van-der-vlist.com/ns/library"
  xmlns:hr="http://eric.van-der-vlist.com/ns/person">
  <!-- The default target namespace is "http://eric.van-der-vlist.com/ns/library" -->
  <oneOrMore>
   <element name="book">
    <attribute name="id"/>
    <attribute name="available"/>
    <element name="isbn">
     <text/>
    </element>
    <element name="title">
     <attribute name="xml:lang"/>
     <text/>
    </element>
    <zeroOrMore>
     <element name="hr:author">
     <!-- Here we are using a "hr" prefix to match "http://eric.van-der-vlist.com/ns/person" -->
      <attribute name="id"/>
      <element name="hr:name">
       <text/>
      </element>
      <element name="hr:born">
       <text/>
      </element>
      <optional>
       <element name="hr:died">
        <text/>
       </element>
      </optional>
     </element>
    </zeroOrMore>
    <zeroOrMore>
     <element name="character">
      <attribute name="id"/>
      <element name="hr:name">
       <text/>
      </element>
      <element name="hr:born">
       <text/>
      </element>
      <element name="qualification">
       <text/>
      </element>
     </element>
    </zeroOrMore>
   </element>
  </oneOrMore>
 </element>]]>
      </programlisting>
      <para>A namespace declaration is provided in the compact syntax to define namespace prefixes:</para>
      <programlisting>
<![CDATA[ default namespace = "http://eric.van-der-vlist.com/ns/library"
 namespace hr = "http://eric.van-der-vlist.com/ns/person"]]>
      </programlisting>
      <programlisting>
<![CDATA[
 element library
 {
    element book
    {
       attribute id { text },
       attribute available { text },
       element isbn { text },
       element title { attribute xml:lang { text }, text },
       element hr:author
       {
          attribute id { text },
          element hr:name { text },
          element hr:born { text },
          element hr:died { text }?
       }*,
       element character
       {
          attribute id { text },
          element hr:name { text },
          element hr:born { text },
          element qualification { text }
       }*
    }+
 }]]>
      </programlisting>
      <para>Again, this schema will validate the three variations seen in the &quot;10 minutes guide&quot;. In fact this schema validates exactly the same set of document than the schema using only default namespaces. The third variation would be to use prefixes for both namespaces:</para>
      <programlisting>
<![CDATA[ <?xml version="1.0" encoding="utf-8"?>
 <element xmlns="http://relaxng.org/ns/structure/1.0"
  name="lib:library"
  xmlns:lib="http://eric.van-der-vlist.com/ns/library"
  xmlns:hr="http://eric.van-der-vlist.com/ns/person">
  <oneOrMore>
   <element name="lib:book">
    <attribute name="id"/>
    <attribute name="available"/>
    <element name="lib:isbn">
     <text/>
    </element>
    <element name="lib:title">
     <attribute name="xml:lang"/>
     <text/>
    </element>
    <zeroOrMore>
     <element name="hr:author">
      <attribute name="id"/>
      <element name="hr:name">
       <text/>
      </element>
      <element name="hr:born">
       <text/>
      </element>
      <optional>
       <element name="hr:died">
        <text/>
       </element>
      </optional>
     </element>
    </zeroOrMore>
    <zeroOrMore>
     <element name="lib:character">
      <attribute name="id"/>
      <element name="hr:name">
       <text/>
      </element>
      <element name="hr:born">
       <text/>
      </element>
      <element name="lib:qualification">
       <text/>
      </element>
     </element>
    </zeroOrMore>
   </element>
  </oneOrMore>
 </element>]]>
      </programlisting>
      <para>or:</para>
      <programlisting>
<![CDATA[ namespace lib = "http://eric.van-der-vlist.com/ns/library"
 namespace hr = "http://eric.van-der-vlist.com/ns/person"]]>
      </programlisting>
      <programlisting>
<![CDATA[
 element lib:library
 {
    element lib:book
    {
       attribute id { text },
       attribute available { text },
       element lib:isbn { text },
       element lib:title { attribute xml:lang { text }, text },
       element hr:author
       {
          attribute id { text },
          element hr:name { text },
          element hr:born { text },
          element hr:died { text }?
       }*,
       element lib:character
       {
          attribute id { text },
          element hr:name { text },
          element hr:born { text },
          element lib:qualification { text }
       }*
    }+
 }]]>
      </programlisting>
      <para>And again, this schema is equivalent to the previous ones since it's validating all the variations of namespaces declarations in the instance documents.</para>
    </sect2>
  </sect1>
  <sect1>
    <title>Accepting &quot;foreign namespaces&quot;</title>
    <para>The last schemas which we have seen will validate instance documents independently of the prefixes being used and they meet the first goals of the namespaces which are to disambiguate elements in multi-namespaces documents. However, they will fail to validate the instance document where we've added the <literal>dc:publisher</literal> element. We could easily update our schema to explicitly add this element to the content model of our <literal>book</literal> element, but that wouldn't make it an open schema accepting addition of elements from any other namespace.</para>
    <para>Instead of some &quot;magic feature&quot; which would probably have been quite rigid, Relax NG has introduced a flexible and clever feature to let you define your own level of &quot;openness&quot;. The idea to do so is to let you define your own &quot;wildcard&quot; and once you get it, you can include it wherever you want in your content model.</para>
    <sect2>
      <title>Constructing our wildcard</title>
      <para>Before we start, let's define what we are trying to achieve! We want a named pattern allowing any element or attribute which do not belong to our <literal>lib</literal> and <literal>hr</literal> namespaces. We probably want to exclude elements and attributes with no namespaces: attributes because our own attributes have no namespace and we might want to differentiate them and elements because allowing elements without namespaces in a document using namespaces is kind of messy.</para>
      <para>For the content model of these foreign elements, we have two main options:</para>
      <itemizedlist>
        <listitem><para>We can be liberal and accept anything including elements and attributes from the namespaces described in the schema.</para></listitem>
        <listitem><para>We can be more conservative and accept only foreign elements.</para></listitem>
      </itemizedlist>
      <para>If we want to be liberal, we must define the inner content of the wildcard to formalize what <literal>anything</literal> is. In this case, this can be expressed as any number of elements (themselves containing <literal>anything</literal>), attributes and text in any order and this is a good candidate for a recursive named pattern:</para>
      <programlisting>
<![CDATA[  <define name="anything">
    <zeroOrMore>
      <choice>
        <element>
          <anyName/>
          <ref name="anything"/>
        </element>
        <attribute>
          <anyName/>
        </attribute>
        <text/>
      </choice>
    </zeroOrMore>
  </define>]]>
      </programlisting>
      <para>or:</para>
      <programlisting>
<![CDATA[ anything = ( element * { anything } | attribute * { text } | text )*]]>
      </programlisting>
      <para>The only thing new here is the <literal>anyName</literal> element (XML syntax) or <literal>*</literal> operator (compact syntax) to replace the name of an element or attribute. This is the first example of a &quot;name class&quot; (i.e. a class of names) and we'll see that there are many ways to restrict this name class. Now that we have a named pattern to express what <literal>anything</literal> is, we can use it to define what &quot;foreign&quot; elements mean:</para>
      <programlisting>
<![CDATA[  <define name="foreign-elements">
    <zeroOrMore>
      <element>
        <anyName>
          <except>
            <nsName ns=""/>
            <nsName ns="http://eric.van-der-vlist.com/ns/library"/>
            <nsName ns="http://eric.van-der-vlist.com/ns/person"/>
          </except>
        </anyName>
        <ref name="anything"/>
      </element>
    </zeroOrMore>
  </define>]]>
      </programlisting>
      <para>or:</para>
      <programlisting>
<![CDATA[ default namespace lib = "http://eric.van-der-vlist.com/ns/library"
 namespace local = ""
 namespace hr = "http://eric.van-der-vlist.com/ns/person"]]>
      </programlisting>
      <programlisting>
<![CDATA[
 .../...]]>
      </programlisting>
      <programlisting>
<![CDATA[
 foreign-elements = element * - (local:* | lib:* | hr:*) { anything }*]]>
      </programlisting>
      <para>To achieve our purpose, we have introduced two new elements embedded in the <literal>anyName</literal> name class: <literal>except</literal> (or <literal>-</literal> in the compact syntax) which has here the same meaning than with enumerations and <literal>nsName</literal> (xxx:* in the compact syntax) which means &quot;any name from a namespace&quot;. When using the XML syntax, <literal>nsName</literal> takes a <literal>ns</literal> attribute while prefixes are used when using the compact syntax. This usage of prefixes in the compact syntax implies that declarations are added to define prefixes for the <literal>lib</literal> (which is also the default namespace) and <literal>hr</literal> namespaces but also for &quot;no namespace&quot; (here we have used the prefix <literal>local</literal>).</para>
      <para>Note that name classes are not considered as patterns but as a specific set of elements with a specific purpose. A consequence of this statement is that name classes definitions cannot be placed within named patterns to be reused and that we had to repeat the same name class for both elements and attributes.</para>
      <para>The same can be done to define foreign attributes:</para>
      <programlisting>
<![CDATA[  <define name="foreign-attributes">
    <zeroOrMore>
      <attribute>
        <anyName>
          <except>
            <nsName ns=""/>
            <nsName ns="http://eric.van-der-vlist.com/ns/library"/>
            <nsName ns="http://eric.van-der-vlist.com/ns/person"/>
          </except>
        </anyName>
      </attribute>
    </zeroOrMore>
  </define>]]>
      </programlisting>
      <para>or:</para>
      <programlisting>
<![CDATA[ foreign-attributes = attribute * - (local:* | lib:* | hr:*) { text }*]]>
      </programlisting>
      <para>And for our convenience, we can also define foreign nodes by combining foreign elements and attributes:</para>
      <programlisting>
<![CDATA[  <define name="foreign-nodes">
    <zeroOrMore>
      <choice>
        <ref name="foreign-attributes"/>
        <ref name="foreign-elements"/>
      </choice>
    </zeroOrMore>
  </define>]]>
      </programlisting>
      <para>or:</para>
      <programlisting>
<![CDATA[ foreign-nodes = ( foreign-attributes | foreign-elements )*]]>
      </programlisting>
    </sect2>
    <sect2>
      <title>Using our wildcard</title>
      <para>So far so good and now that we have defined what <literal>foreign-nodes</literal> are, we can use it to give more extensibility to our schema. To enable foreign-nodes where we have added the <literal>dc:publisher</literal> element, i.e. between the <literal>title</literal> and <literal>author</literal> elements, we could write (switching to a &quot;flatter&quot; style to make it more readable):</para>
      <programlisting>
<![CDATA[     <element name="book">
      <attribute name="id"/>
      <attribute name="available"/>
      <ref name="isbn-element"/>
      <ref name="title-element"/>
      <ref name="foreign-nodes"/>
      <zeroOrMore>
        <ref name="author-element"/>
      </zeroOrMore>
      <zeroOrMore>
        <ref name="character-element"/>
      </zeroOrMore>
    </element>]]>
      </programlisting>
      <para>or:</para>
      <programlisting>
<![CDATA[ book-element =
    element book
    {
       attribute id { text },
       attribute available { text },
       isbn-element,
       title-element,
       foreign-nodes,
       author-element*,
       character-element*
    }]]>
      </programlisting>
      <para>This would do the trick for the instance document shown above, but wouldn't validate a document where foreign nodes would be added at any other place, for instance between the <literal>isbn</literal> and <literal>title</literal> elements. We could insert a reference to the <literal>foreign-nodes</literal> pattern between all the elements, but this would be very verbose and if we think about it, we want here to <literal>interleave</literal> these foreign nodes between the content defined for the <literal>book</literal> element and that's a good opportunity to use the <literal>interleave</literal> pattern:</para>
      <programlisting>
<![CDATA[    <element name="book">
      <interleave>
        <group>
          <attribute name="id"/>
          <attribute name="available"/>
          <ref name="isbn-element"/>
          <ref name="title-element"/>
          <zeroOrMore>
            <ref name="author-element"/>
          </zeroOrMore>
          <zeroOrMore>
            <ref name="character-element"/>
          </zeroOrMore>
        </group>
        <ref name="foreign-nodes"/>
      </interleave>
    </element>]]>
      </programlisting>
      <para>or:</para>
      <programlisting>
<![CDATA[    element book
    {
       (
          attribute id { text },
          attribute available { text },
          isbn-element,
          title-element,
          author-element*,
          character-element*
       )
     & foreign-nodes
    }]]>
      </programlisting>
      <para>As seen in &quot;Chapter 6: More Patterns&quot;, foreign nodes will be interleaved in the group which was previously defining the content of the book element: the order of the group is still enforced but foreign nodes may appear in any order and everywhere.</para>
    </sect2>
    <sect2>
      <title>Where should we allow foreign nodes?</title>
      <para>We may be tempted to allow these foreign nodes everywhere in our document, however if the extensibility which would be given is usually fine in elements such as book which already have children element, it's often considered a bad practice to do the same in elements which content is text only, such as the <literal>isbn</literal> element where this would transform a text content models into a mixed content model.</para>
      <para>This is due to the weak support for mixed content models which we've already mentioned in &quot;Chapter 6: More patterns&quot; when we've discussed the limitations of the <literal>mixed</literal> pattern. A consequence of allowing foreign elements in <literal>isbn</literal> elements would be that the content of this element could not be considered a <literal>data</literal> any longer and that no datatypes nor restrictions could be applied any longer.</para>
      <para>Beyond the limitation of Relax NG, applications would have to concatenate text nodes spread over the foreign elements and this is may be pretty verbose with tools such as XPath and XSLT.</para>
      <para>A compromise which is often taken is to allow only foreign attributes in text content models, but that's not an issue for us since our <literal>foreign-attributes</literal> is ready for this purpose:</para>
      <programlisting>
<![CDATA[    <element name="isbn">
      <ref name="foreign-attributes"/>
      <text/>
    </element>]]>
      </programlisting>
      <para>or:</para>
      <programlisting>
<![CDATA[ element isbn { foreign-attributes, text }]]>
      </programlisting>
    </sect2>
    <sect2>
      <title>A couple of traps to avoid</title>
      <para>If most of the time, using our wildcards is straightforward there are some situations where this may lead to unexpected schema errors, especially with attributes which usage is subject to restrictions.</para>
      <para>The first of the traps which I'd like to mention here is related to the fact that the definition of attributes cannot be duplicated in a schema and the following definition would be invalid:</para>
      <programlisting>
<![CDATA[ element title { attribute xml:space, attribute xml:space, text } # this is invalid]]>
      </programlisting>
      <para>This seems to be pretty sensible since duplicate attributes are forbidden in the instance document. Unfortunately, the attribute <literal>xml:space</literal> is allowed by our <literal>foreign-attributes</literal> named template and we will get an error as well if we extend the definition of our title element without taking care and write:</para>
      <programlisting>
<![CDATA[ element title { foreign-attributes, attribute xml:space, text } # this is also invalid]]>
      </programlisting>
      <para>To fix this error, we will need either to remove the xml:space attribute from the name class of our foreign attributes or to remove the implicit mention of xml:space in our definition and just write:</para>
      <programlisting>
<![CDATA[ element title { foreign-attributes, text }]]>
      </programlisting>
      <para>Of course, this doesn't remove the possibility to include a xml:space attribute in the <literal>title</literal> element since this attribute is a &quot;foreign attribute&quot; as defined in our named pattern.</para>
      <para>The second trap is a level higher on the same line and is specific to the DTD compatibility <literal>ID</literal> datatype. In &quot;Chapter 8: Datatype libraries&quot;, when we have seen this datatype, we have used it to define the book element:</para>
      <programlisting>
<![CDATA[   <element name="book">
    <attribute name="id">
     <data datatypeLibrary="http://relaxng.org/ns/compatibility/datatypes/1.0" type="ID"/>
    </attribute>
    .../...
   </element>]]>
      </programlisting>
      <para>or:</para>
      <programlisting>
<![CDATA[  element book {
   attribute id {dtd:ID},
   .../...
  }]]>
      </programlisting>
      <para>Here again, we will generate an error if we add our foreign nodes and the reason for this error is that this datatype is emulating the DTD in all its aspects including the fact that if an element <literal>book</literal> is defined with an <literal>id</literal> attribute having a type <literal>ID</literal>, all the other definitions of an attribute <literal>id</literal> hosted by an element <literal>book</literal> must have the same type <literal>ID</literal>. The problem here is that, hidden in the definition of <literal>anything</literal>, there can be elements <literal>book</literal> having an attribute <literal>id</literal> of type <literal>text</literal> and this is considered as an error.</para>
      <para>The only workaround if we want to use the DTD type <literal>ID</literal> is to remove this possibility from the named pattern <literal>anything</literal>. A fast solution would be to exclude our namespaces from the class names in <literal>anything</literal> but we can find more elaborated constructions with the elements which will be introduced in the next chapter.</para>
    </sect2>
    <sect2>
      <title>Adding foreign nodes through combination</title>
      <para>To add our foreign nodes, we have transformed:</para>
      <programlisting>
<![CDATA[    <element name="book">
      <attribute name="id"/>
      <attribute name="available"/>
      <ref name="isbn-element"/>
      <ref name="title-element"/>
      <zeroOrMore>
        <ref name="author-element"/>
      </zeroOrMore>
      <zeroOrMore>
        <ref name="character-element"/>
      </zeroOrMore>
    </element>]]>
      </programlisting>
      <para>or:</para>
      <programlisting>
<![CDATA[    element book
    {
      attribute id { text },
      attribute available { text }
      isbn-element,
      title-element,
      author-element*,
      character-element*
    }]]>
      </programlisting>
      <para>into:</para>
      <programlisting>
<![CDATA[    <element name="book">
      <interleave>
        <group>
          <attribute name="id"/>
          <attribute name="available"/>
          <ref name="isbn-element"/>
          <ref name="title-element"/>
          <zeroOrMore>
            <ref name="author-element"/>
          </zeroOrMore>
          <zeroOrMore>
            <ref name="character-element"/>
          </zeroOrMore>
        </group>
        <ref name="foreign-nodes"/>
      </interleave>
    </element>]]>
      </programlisting>
      <para>or:</para>
      <programlisting>
<![CDATA[    element book
    {
       (
          attribute id { text },
          attribute available { text },
          isbn-element,
          title-element,
          author-element*,
          character-element*
       )
     & foreign-nodes
    }]]>
      </programlisting>
      <para>and this operation could be done as a pattern combination by interleave if the content of the element <literal>book</literal> is described as a named pattern:</para>
      <programlisting>
<![CDATA[  <define name="book-content">
    <attribute name="id"/>
    <attribute name="available"/>
    <ref name="isbn-element"/>
    <ref name="title-element"/>
    <zeroOrMore>
      <ref name="author-element"/>
    </zeroOrMore>
    <zeroOrMore>
      <ref name="character-element"/>
    </zeroOrMore>
  </define>]]>
      </programlisting>
      <para>or:</para>
      <programlisting>
<![CDATA[ book-content =
    attribute id { text },
    attribute available { text },
    isbn-element,
    title-element,
    author-element*,
    character-element*]]>
      </programlisting>
      <para>This pattern can then easily be extended as:</para>
      <programlisting>
<![CDATA[  <define name="book-content" combine="interleave">
    <ref name="foreign-nodes"/>
  </define>]]>
      </programlisting>
      <para>or</para>
      <programlisting>
<![CDATA[ book-content &= foreign-nodes]]>
      </programlisting>
      <para>and used to define the <literal>book</literal> element:</para>
      <programlisting>
<![CDATA[    <element name="book">
      <ref name="book-content"/>
    </element>]]>
      </programlisting>
      <para>or:</para>
      <programlisting>
<![CDATA[ element book { book-content }]]>
      </programlisting>
      <para>This combination can be done in a single document but this mechanism can also be used to extend a vocabulary through merging a grammar containing only these combinations.</para>
      <para>Note that the exact same combination does also work for appending foreign attributes to the elements which have a text only content model.</para>
    </sect2>
  </sect1>
  <sect1>
    <title>Namespaces and building blocks, chameleon design</title>
    <sect2>
      <title>Back to XHTML 2.0</title>
      <para>In &quot;Chapter 10: Creating Building Blocks&quot;, we've seen the example of the schemas for XHTML 2.0 and I have urged you not to worry about the namespace declarations which hadn't been introduced yet. Let's have a closer look now that we know how to declare namespaces.</para>
      <para>If we look for namespace declarations in the top level schema (the &quot;driver&quot;), we will find them only in the grammar element:</para>
      <programlisting>
<![CDATA[ <?xml version="1.0" encoding="UTF-8"?>
 <grammar ns="http://www.w3.org/2002/06/xhtml2"
         xmlns="http://relaxng.org/ns/structure/1.0"
         xmlns:x="http://www.w3.org/1999/xhtml">]]>
      </programlisting>
      <programlisting>
<![CDATA[
  <x:h1>RELAX NG schema for XHTML 2.0</x:h1>
  .../...
    <x:h3>Structure Module</x:h3>
    <include href="xhtml-struct-2.rng"/>
   .../...
 </grammar>]]>
      </programlisting>
      <para>What do we see in this snippet?</para>
      <itemizedlist>
        <listitem><para> xmlns=&quot;http://relaxng.org/ns/structure/1.0&quot; means that the default namespace of the schema as a XML document is &quot;http://relaxng.org/ns/structure/1.0&quot;. That just means that elements without prefix in the schema as a XML document are Relax NG patterns.</para></listitem>
        <listitem><para> ns=&quot;http://www.w3.org/2002/06/xhtml2&quot; defines the default namespace for the schema itself: the schema describes elements from the &quot;http://www.w3.org/2002/06/xhtml2&quot; namespace unless some other namespace is explicitly defined. Let's call it the &quot;target namespace&quot; to avoid any confusion with the default namespace of the schema considered as a XML document.</para></listitem>
        <listitem><para> xmlns:x=&quot;http://www.w3.org/1999/xhtml&quot; defines that the prefix &quot;x&quot; is assigned to &quot;http://www.w3.org/1999/xhtml&quot;. This declaration is used here to include XHTML documentation in the schema and we will see this in more detail in &quot;Chapter 13: Annotating Schemas&quot;.</para></listitem>
      </itemizedlist>
      <para>This would translate in the compact syntax as:</para>
      <programlisting>
<![CDATA[ default namespace = "http://www.w3.org/2002/06/xhtml2"
 namespace x = "http://www.w3.org/1999/xhtml"]]>
      </programlisting>
      <programlisting>
<![CDATA[
 .../...]]>
      </programlisting>
      <programlisting>
<![CDATA[
 include "xhtml-struct-2.rnc"]]>
      </programlisting>
      <para>Let's now have a look at the module describing the structure:</para>
      <programlisting>
<![CDATA[ <?xml version="1.0" encoding="UTF-8"?>
 <grammar xmlns="http://relaxng.org/ns/structure/1.0"
         xmlns:x="http://www.w3.org/1999/xhtml">]]>
      </programlisting>
      <programlisting>
<![CDATA[
   <x:h1>Structure Module</x:h1>
   .../...]]>
      </programlisting>
      <programlisting>
<![CDATA[
 </grammar>]]>
      </programlisting>
      <para>Or:</para>
      <programlisting>
<![CDATA[ namespace x = "http://www.w3.org/1999/xhtml"]]>
      </programlisting>
      <programlisting>
<![CDATA[
 x:h1 [ "Structure Module" ]
 start = html
 ...]]>
      </programlisting>
      <para>The big difference with the top level schema is that the target namespace isn't defined in the schema defining the module.</para>
      <para>How can that work? It's a feature common to the <literal>include</literal> and <literal>externalRef</literal> pattern than when no target namespace is defined in the imported schema, the target namespace from the schema performing the inclusion or external reference is used. In our case, that means that the target namespace from the driver (&quot;http://www.w3.org/2002/06/xhtml2&quot;) is used by any module which do no specify a target namespace.</para>
      <para>For this reason, schemas without target namespace are often called &quot;chameleon schemas&quot; since they take the target namespace of any context in which they are included or referenced.</para>
      <para>In the compact syntax, an <literal>inherit</literal> qualifier has been added to specify a namespace must be inherited at inclusion or external reference time:</para>
      <programlisting>
<![CDATA[ namespace xhtml2 = "http://www.w3.org/2002/06/xhtml2"
 namespace x = "http://www.w3.org/1999/xhtml"]]>
      </programlisting>
      <programlisting>
<![CDATA[
 .../...]]>
      </programlisting>
      <programlisting>
<![CDATA[
 include "xhtml-struct-2.rnc" inherit = xhtml2]]>
      </programlisting>
      <para>This <literal>inherit</literal> qualifier has the same role as a <literal>ns</literal> attribute in an <literal>include</literal> or externalRef&quot; of the XML syntax.</para>
    </sect2>
    <sect2>
      <title>Applicability to our library</title>
      <para>Back to XHTML 2.0 and our library, we may want to include XHTML elements into the description of our library, for instance to allow the same content for the definition of our titles and qualification than in the XHTML <literal>p</literal> element, i.e. what's described in the &quot;Inline text&quot; module as the <literal>Inline.model</literal> named pattern. The idea beside this mechanism of modules is that we can select the modules we'll want to include and we can follow this principle to pick just what we need and that's just what we'll do by including the common modules (&quot;xhtml-attribs-2.rng&quot; and &quot;xhtml-datatypes-2.rng&quot;) and the &quot;Inline Text&quot; module (<literal></literal>xhtml-inltext-2.rng&quot;):</para>
      <programlisting>
<![CDATA[ <?xml version="1.0" encoding="UTF-8"?>
 <grammar xmlns="http://relaxng.org/ns/structure/1.0">
   <start>
     <ref name="library"/>
   </start>
   <include href="xhtml-attribs-2.rng"/>
   <include href="xhtml-inltext-2.rng"/>
   <include href="xhtml-datatypes-2.rng"/>
   <define name="library">
     <element name="library">
       <oneOrMore>
         <element name="book">
           <attribute name="id"/>
           <attribute name="available"/>
           <element name="isbn">
             <text/>
           </element>
           <element name="title">
             <attribute name="xml:lang"/>
             <ref name="Inline.model"/>
           </element>
           <oneOrMore>
             <element name="author">
               <attribute name="id"/>
               <element name="name">
                 <text/>
               </element>
               <optional>
                 <element name="born">
                   <text/>
                 </element>
               </optional>
               <optional>
                 <element name="died">
                   <text/>
                 </element>
               </optional>
             </element>
           </oneOrMore>
           <zeroOrMore>
             <element name="character">
               <attribute name="id"/>
               <element name="name">
                 <text/>
               </element>
               <optional>
                 <element name="born">
                   <text/>
                 </element>
               </optional>
               <element name="qualification">
                 <ref name="Inline.model"/>
               </element>
             </element>
           </zeroOrMore>
         </element>
       </oneOrMore>
     </element>
   </define>
 </grammar>]]>
      </programlisting>
      <para>Or:</para>
      <programlisting>
<![CDATA[ start = library
 include "xhtml-attribs-2.rnc"
 include "xhtml-inltext-2.rnc"
 include "xhtml-datatypes-2.rnc"
 library =
   element library {
     element book {
       attribute id { text },
       attribute available { text },
       element isbn { text },
       element title {
         attribute xml:lang { text },
         Inline.model
       },
       element author {
         attribute id { text },
         element name { text },
         element born { text }?,
         element died { text }?
       }+,
       element character {
         attribute id { text },
         element name { text },
         element born { text }?,
         element qualification { Inline.model }
       }*
     }+
   }]]>
      </programlisting>
      <para>With this schema, I can include all the XHTML formatting described in the &quot;Inline Text Module&quot; in my <literal>title</literal> and <literal>qualification</literal> elements, but since they must be in the target namespace defined in this schema (i.e. with no namespace since I haven't defined a target namespace here). The local names of the elements are thus the same than those of XHTML 2.0 but these elements must have no namespace. An example of valid document could be:</para>
      <programlisting>
<![CDATA[ <?xml version="1.0" encoding="utf-8"?>
 <library>
   <book id="b0836217462" available="true">
     <isbn>0836217462</isbn>
     <title xml:lang="en">Being a Dog Is a <em>Full-Time Job</em></title>
     <author id="CMS">
       <name>Charles M Schulz</name>
       <born>1922-11-26</born>
       <died>2000-02-12</died>
     </author>
     <character id="PP">
       <name>Peppermint Patty</name>
       <born>1966-08-22</born>
       <qualification>bold, brash and tomboyish</qualification>
     </character>"http://www.w3.org/2002/06/xhtml2"
     <character id="Snoopy">
       <name>Snoopy</name>
       <born>1950-10-04</born>
       <qualification>extroverted <strong>beagle</strong></qualification>
     </character>
     <character id="Schroeder">
       <name>Schroeder</name>
       <born>1951-05-30</born>
       <qualification>brought classical music to the Peanuts strip</qualification>
     </character>
     <character id="Lucy">
       <name>Lucy</name>
       <born>1952-03-03</born>
       <qualification>bossy, crabby and selfish</qualification>
     </character>
   </book>
 </library>]]>
      </programlisting>
      <para>Because the XHTML 2.0 schemas for the modules are chameleon schemas, to import the definitions from XHTML in their namespaces, I need to specify this namespace in my <literal>include</literal> patterns:</para>
      <programlisting>
<![CDATA[ <?xml version="1.0" encoding="UTF-8"?>
 <grammar xmlns="http://relaxng.org/ns/structure/1.0">
   <start>
     <ref name="library"/>
   </start>
   <include href="xhtml-attribs-2.rng" ns="http://www.w3.org/2002/06/xhtml2"/>
   <include href="xhtml-inltext-2.rng" ns="http://www.w3.org/2002/06/xhtml2"/>
   <include href="xhtml-datatypes-2.rng" ns="http://www.w3.org/2002/06/xhtml2"/>
   <define name="library">
     <element name="library">
       <oneOrMore>
         <element name="book">
           <attribute name="id"/>
           <attribute name="available"/>
           <element name="isbn">
             <text/>which
           </element>
           <element name="title">
             <attribute name="xml:lang"/>
             <ref name="Inline.model"/>
           </element>
           <oneOrMore>
             <element name="author">
               <attribute name="id"/>
               <element name="name">
                 <text/>
               </element>
               <optional>
                 <element name="born">
                   <text/>
                 </element>
               </optional>
               <optional>
                 <element name="died">
                   <text/>
                 </element>
               </optional>
             </element>
           </oneOrMore>
           <zeroOrMore>
             <element name="character">
               <attribute name="id"/>
               <element name="name">
                 <text/>
               </element>
               <optional>
                 <element name="born">
                   <text/>
                 </element>
               </optional>
               <element name="qualification">
                 <ref name="Inline.model"/>
               </element>
             </element>
           </zeroOrMore>
         </element>
       </oneOrMore>
     </element>
   </define>
 </grammar>]]>
      </programlisting>
      <para>Or:</para>
      <programlisting>
<![CDATA[ namespace x = "http://www.w3.org/2002/06/xhtml2"]]>
      </programlisting>
      <programlisting>
<![CDATA[
 start = library
 include "xhtml-attribs-2.rnc" inherit = x
 include "xhtml-inltext-2.rnc" inherit = x
 include "xhtml-datatypes-2.rnc" inherit = x
 library =
   element library {
     element book {
       attribute id { text },
       attribute available { text },
       element isbn { text },
       element title {
         attribute xml:lang { text },
         Inline.model
       },
       element author {
         attribute id { text },
         element name { text },
         element born { text }?,
         element died { text }?
       }+,
       element character {
         attribute id { text },
         element name { text },
         element born { text }?,
         element qualification { Inline.model }
       }*
     }+
   }]]>
      </programlisting>
      <para>The namespace which is inherited is now explicitly set to &quot;http://www.w3.org/2002/06/xhtml2&quot; and valid documents look like:</para>
      <programlisting>
<![CDATA[ <?xml version="1.0" encoding="utf-8"?>
 <library xmlns:x="http://www.w3.org/2002/06/xhtml2">
   <book id="b0836217462" available="true">
     <isbn>0836217462</isbn>
     <title xml:lang="en">Being a Dog Is a <x:em>Full-Time Job</x:em></title>
     <author id="CMS">
       <name>Charles M Schulz</name>
       <born>1922-11-26</born>
       <died>2000-02-12</died>
     </author>
     <character id="PP">
       <name>Peppermint Patty</name>
       <born>1966-08-22</born>
       <qualification>bold, brash and tomboyish</qualification>
     </character>
     <character id="Snoopy">
       <name>Snoopy</name>
       <born>1950-10-04</born>
       <qualification>extroverted <x:strong>beagle</x:strong></qualification>
     </character>
     <character id="Schroeder">
       <name>Schroeder</name>
       <born>1951-05-30</born>
       <qualification>brought classical music to the Peanuts strip</qualification>
     </character>
     <character id="Lucy">
       <name>Lucy</name>
       <born>1952-03-03</born>
       <qualification>bossy, crabby and selfish</qualification>
     </character>
   </book>
 </library>]]>
      </programlisting>
    </sect2>
    <sect2>
      <title>Good or evil?</title>
      <para>Chameleon schemas are controversial and I am not a big fan of them. On the bright side, they look very handy. The first variation of XHTML inclusion in our library is more concise than the second one where we need to declare the XHTML namespace in each document and add a prefix to XHTML elements. On the other hand, one can question the benefit of adding XHTML elements if they can be identified as XHTML by their namespace. Yes, we can add <literal>em</literal> or <literal>strong</literal> elements in our <literal>title</literal> and <literal>qualification</literal> elements, but how can an application recognize them as XHTML document if they have no namespace or belong to the namespace of our own application?</para>
      <para>Chameleon schemas kind of abuse namespaces to remove most of the interest of using them and for this reason I would recommend to be very cautious when you are using them!</para>
    </sect2>
  </sect1>
</chapter>
